package  
{
	import com.photonstorm.flixel.FlxStarField;
	
	import org.flixel.*;

	public class ScrollingBackground extends FlxGroup
	{
		[Embed(source = '../assets/mapCSV_Group1_Map1.csv', mimeType = 'application/octet-stream')] private var map1CSV:Class;
		[Embed(source = '../assets/tiles.png')] private var tilesPNG:Class;
		
		private var map:FlxTilemap;
		private var dolly:FlxSprite;
		private var stars:FlxStarField;
		
		public function ScrollingBackground() 
		{
			super();
			
			//	The starfield behind the map
			stars = new FlxStarField(0, 0, FlxG.width, FlxG.height, 200, 1);
			stars.setStarSpeed(0, 0.5);
			
			//	Our scrolling background map, created using DAME
			map = new FlxTilemap();
			map.loadMap(new map1CSV, tilesPNG, 16, 16);
			
			map.scrollFactor.x = 0.5;
			map.scrollFactor.y = 1;
			
			//	This is an invisible sprite that our scrolling background tracks
			dolly = new FlxSprite(180, map.height);
			
			dolly.visible = false;
			
			//	Tell Flixels camera system to follow this sprite
			//	Call this AFTER setting the dolly coordinates to avoid the "camera panning to sprite" effect
			FlxG.follow(dolly, 1);
			
			add(stars);
			add(map);
			add(dolly);
		}
		
		override public function update():void
		{
			super.update();
			
			dolly.velocity.y = -20;
			
			//	Have we scrolled off the top of our map?
			if (dolly.y < -480)
			{
				//	Yes, so let's reset back to the start again (we unfollow first to stop camera jittering)
				FlxG.unfollow();
				dolly.y = map.height;
				FlxG.follow(dolly, 1);
			}
		}
	}

}